package MAP;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

/**
 * Date: 2023-04-19
 * Time: 14:00
 */

// 第二种遍历方式  通过键值对对象进行遍历

public class MapDemo3 {
    public static void main(String[] args) {
        // 创建对象
        Map<String,String> map = new HashMap<>();

        map.put("齐天大圣","孙悟空");
        map.put("鸡你太美","蔡徐坤");
        map.put("空你几哇","小日本");

        //通过一个方法,获取所有键值对对象,返回一个set集合
        Set<Map.Entry<String, String>> entries = map.entrySet();
        // Map.Entry<String, String> 类型
        // 强调for循环
        System.out.println("-------------------for------------------");
        for(Map.Entry<String,String> entry: entries){

            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+" "+value);
        }

        // 迭代器
        // 创建迭代器对象
        System.out.println("-------------------iterator------------------");
        Iterator<Map.Entry<String, String>> it = entries.iterator();

        while(it.hasNext()){
            Map.Entry<String, String> next = it.next();
            System.out.println(next.getKey()+" "+next.getValue());
        }

        // lambda 表达式
        System.out.println("--------------------lambda----------------------");
        entries.forEach(next-> System.out.println(next.getKey()+" "+next.getValue()));

        // 匿名内部类
        entries.forEach(new Consumer<Map.Entry<String, String>>() {
            @Override
            public void accept(Map.Entry<String, String> stringStringEntry) {
                System.out.println(stringStringEntry.getKey()+" "+stringStringEntry.getValue());
            }
        });
    }
}
